package eclipse.weibo.modle.control;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import eclipse.weibo.util.ReadImageUtil;

import weibo4j.util.BareBonesBrowserLaunch;
import weibo4j.util.DebugPrn;

public class IncreaseLabel extends Composite {
    private static DebugPrn dp = new DebugPrn(IncreaseLabel.class.getName());
    private static final Cursor cursor = Display.getDefault().getSystemCursor(SWT.CURSOR_HAND);
    private int widthHint = 250;
    private Composite parent;
    private Composite c1 = null;
    private Composite c2 = null;
    private GridData o1gd1 = null;
    private GridData o1gd2 = null;
    private Label userProfileImage = null;
    private Label userImage = null;
    private String original_pic;
    private Link userName = null;
    private Text uerText = null;
    private RetweetComposite retweetLabel = null;

    public IncreaseLabel(Composite parent) {
        super(parent, SWT.WRAP | SWT.BORDER);
        this.parent = parent;
        this.setLayout(new RowLayout());
        c1 = new Composite(this, SWT.NONE);
        c2 = new Composite(this, SWT.NONE);
        c1.setLayout(new GridLayout(1, false));
        c2.setLayout(new GridLayout(1, false));
    }

    public void setUserProfileImage(String imageUrl) {
        try {
            setUserProfileImage(new URL(imageUrl));
        } catch (MalformedURLException ex) {
            dp.error(ex, ex);
        }
    }

    public void setUserProfileImage(URL imageUrl) {
        ImageData image = ReadImageUtil.read(imageUrl);
        getUserProfileImage().setImage(new Image(Display.getDefault(), image));
    }

    private Label getUserProfileImage() {
        if (userProfileImage == null) {
            userProfileImage = new Label(c1, SWT.NONE);
        }
        return userProfileImage;
    }

    public void setUserName(String text) {
        //getUserName().setText(text);
    }

    private Link getUserName() {
        if (userName == null) {
            userName = new Link(c1, SWT.NONE);
        }
        return userName;
    }

    public void setUserText(String text) {
        getUerText().setText(text);
    }

    private Text getUerText() {
        if (uerText == null) {
            uerText = new Text(c2, SWT.LEFT | SWT.WRAP);
            uerText.setLayoutData(getUserGridData());
        }
        uerText.setEditable(false);
        return uerText;
    }

    public void setUserImage(String thumbnail_pic, String original_pic) {
        try {
            if (thumbnail_pic != null && thumbnail_pic.length() > 0) {
                this.original_pic = original_pic;
                setUserImage(new URL(thumbnail_pic));
            }
        } catch (MalformedURLException ex) {
            dp.error(ex, ex);
        }
    }

    public void setUserImage(URL imageUrl) {
        ImageData image = ReadImageUtil.read(imageUrl);
        getUserImage().setImage(new Image(Display.getDefault(), image));
    }

    private Label getUserImage() {
        if (userImage == null) {
            userImage = new Label(c2, SWT.NONE);
        }
        userImage.setCursor(cursor);
        userImage.addMouseListener(new ImageMouseListener());
        return userImage;
    }

    public void setRetweet(String text) {
        if (text != null) {
            getRetweetComposite().setRetweetText(text);
        }
    }

    public void setRetweetImage(String thumbnail_pic, String original_pic) {
        if (thumbnail_pic != null && thumbnail_pic.length() > 0) {
            getRetweetComposite().setRetweetImage(thumbnail_pic, original_pic);
        }
    }

    private RetweetComposite getRetweetComposite() {
        if (retweetLabel == null) {
            retweetLabel = new RetweetComposite(c2);
            retweetLabel.setLayoutData(getRetweetGridData());
        }
        return retweetLabel;
    }

    private GridData getUserGridData() {
        if (o1gd1 == null) {
            o1gd1 = new GridData();
            o1gd1.widthHint = widthHint;
        }
        return o1gd1;
    }

    private GridData getRetweetGridData() {
        if (o1gd2 == null) {
            o1gd2 = new GridData();
            o1gd2.widthHint = widthHint;
        }
        return o1gd2;
    }

    private class ImageMouseListener extends MouseAdapter {
        public void mouseDown(MouseEvent e) {
            if (e.button == 1) {
                new ImageDialog(parent.getShell(), original_pic).open();
            }
        }
    }

    private class LinkListener implements SelectionListener {
        public void widgetSelected(SelectionEvent e) {
            BareBonesBrowserLaunch.openURL(e.text);
        }

        public void widgetDefaultSelected(SelectionEvent e) {
        }
    }
}
